jQuery(document).ready(
    function($) {
        var PREFIX='cmc';
        var name = PREFIX+'_license_registration['+PREFIX+'-purchase-code]';
        var val = $("input[name='"+name+"']").val();
        var val_length =val.length;
        if(val_length!=0){
            var subStr = val.toString().substr(0, 9);
            var test = val.toString().substr(-9);
            var results = subStr+'-XXXXXXXX-XXXXXXXX-'+test;
             $("input[name='"+name+"']").attr('value',results);
        }
        let plugins = ["cmc"];
        var url = window.location.href;
        if (url.indexOf('#') > 0) {
            var id = url.indexOf('#');
            $(id).click();
        }
        plugins.forEach(function(PREFIX, index) {
                $(".required [name='" + PREFIX + "_license_registration[" + PREFIX + "-purchase-code]']").attr('required', 'required');
                $(".required [name='" + PREFIX + "_license_registration[" + PREFIX + "-client-emailid]']").attr('required', 'required');
                $(".required [name='" + PREFIX + "_license_registration[" + PREFIX + "-purchase-code]']").attr('oninvalid', 'this.setCustomValidity("Purchase code can not be empty!")');
                $(".required [name='" + PREFIX + "_license_registration[" + PREFIX + "-purchase-code]']").attr('oninput', 'this.setCustomValidity("")');

                $(".required [name='" + PREFIX + "_license_registration[" + PREFIX + "-client-emailid]']").attr('type', 'email');

                if ($("." + PREFIX + "_verification_enable").length > 0) {
                    $("#" + PREFIX + "-activation-button #submit").attr('disabled', 'disabled');
                    $('#' + PREFIX + '-activation-button').addClass('hidden');
                    $('#' + PREFIX + '-verify-permission').addClass('hidden');
                    $('.' + PREFIX + '-notice-red:not(".uninstall")').addClass('hidden');
                    if ($('.'+PREFIX +'_verification_disable').length > 0){                        
                        $('.' + PREFIX + '_verification_disable').prepend(' <a id="'+PREFIX+'-verify-support-status" class="button button-secondary">Verify Support Status</a>')
                    }
                } else {
                    $("#" + PREFIX + "-uninstall-license").attr('disabled', 'disabled');
                    $('#' + PREFIX + '-deactivation-button').addClass('hidden');
                }

                // product uninstall hook
                $("a#" + PREFIX + "-uninstall-license").on('click', function(e) {
                    e.preventDefault();
                    if ($("a#" + PREFIX + "-uninstall-license").attr('disabled')) {
                        return;
                    }
                    let code = $('[id*="' + PREFIX + '-nonce-code"]').val();
                    $.ajax({
                        type: "POST",
                        dataType: "json",
                        url: ajax_object.ajax_url,
                        data: {
                            action: PREFIX + '_uninstall_license',
                            '_password': ajax_object.verify
                        },
                        beforeSend: function(data) {
                            $("a#" + PREFIX + "-uninstall-license").html("Uninstalling license... <span class='cool_timeline-loading'></span>");
                        },
                        success: function(response) {
                            $("a#cool_timeline-uninstall-license").html("License Uninstalled!");
                            alert(response.Message);
                            window.location.reload();
                        }
                    })

                });

            $("a#" + PREFIX + "-verify-support-status").on('click', function (e) {
                e.preventDefault();
                if ($("a#" + PREFIX + "-verify-support-status").attr('disabled')) {
                    return;
                }              
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    url: ajax_object.ajax_url,
                    data: {
                        action: PREFIX + '_verify_support_status',
                        '_password': ajax_object.verify
                    },
                    beforeSend: function (data) {
                        $("a#" + PREFIX + "-verify-support-status").html("Processing... <span class='cool_timeline-loading'></span>");
                    },
                    success: function (response) {
                        $("a#" + PREFIX + "-verify-support-status").html("Refreshed Status!");                     
                        window.location.reload();
                    }
                })

            });      

            }) // end of plugins
    });